In [1]:
using DataFrames
In [2]:
houses = readtable("house_prices.csv")
head(houses)
Out[2]:
In [3]:
standardize(v) = (v - mean(v)) / std(v)
m = size(houses)[1]
y = standardize(houses[:price])
X = [ones(m) standardize(houses[:sqft]) standardize(houses[:rooms])];
In [4]:
h(θ, X) = X * θ
J(θ, X, y) = 0.5 * norm(h(θ, X) - y)
function descend(θ, X, y, α = 0.01)
θ_ = θ - α * X' * (h(θ, X) - y)
θ_, norm(h(θ, X) - y)
end
Out[4]:
In [5]:
function fit(X, y)
θ = zeros(size(X)[2])
θ, cost = descend(θ, X, y)
for i in 1:1000
cost_ = cost
θ, cost = descend(θ, X, y)
if cost_ - cost < 1e-8
break
end
if i % 10 == 0
println("epoch: $i, cost: $cost")
end
end
println("theta: $θ")
end
Out[5]:
In [6]:
fit(X, y)